package com.cmsz.upay;

import com.cmsz.upay.constant.BusinessTypeConstant;
import com.cmsz.upay.constant.OnLineChargingColumnEnum;
import com.cmsz.upay.constant.PayTradeFlowColumnEnum;
import com.cmsz.upay.service.HiveServiceImpl;
import com.cmsz.upay.service.IHiveService;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.serde2.AbstractSerDe;
import org.apache.hadoop.hive.serde2.SerDeException;
import org.apache.hadoop.hive.serde2.SerDeStats;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Properties;

/**
 * Created by le on 2017/12/18.
 * 在线计费业务类型流水表序列化主类
 */
public class OnlineChargingSerdeHive extends AbstractSerDe {
    private static final Logger logger = LoggerFactory.getLogger(OnlineChargingSerdeHive.class);
    // params
    private List<String> columnNames = null;
    private ObjectInspector objectInspector = null;
    private IHiveService hiveService;


    public void initialize(@Nullable Configuration configuration, Properties properties) throws SerDeException {
         hiveService=new HiveServiceImpl();
         columnNames= OnLineChargingColumnEnum.getAllColumnName();
         List<ObjectInspector> columnOIs=hiveService.buildColumnOIS(BusinessTypeConstant.TYPE.ONLINE_CHARGE);
         objectInspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, columnOIs);
    }

    /*将你自定义的Writable对象反序列化成Object的过程*/
    public Object deserialize(Writable writable) throws SerDeException {
        if (writable == null) return null;
         Text text = (Text) writable;
         return hiveService.parsingOnlineChargingXML(text.toString());
    }

    /*通过参数中的ObjectInspector来访问object的具体数据（具体方法如上），然后将这些数据再转化为本SerDe产生的那种Writable类型对象 */
    public Writable serialize(Object o, ObjectInspector objectInspector) throws SerDeException {
        return null;
    }

    /*返回上面所创建的StructObjectInspector对象 */
    public ObjectInspector getObjectInspector() throws SerDeException {
        return objectInspector;
    }

    public SerDeStats getSerDeStats() {
        return null;
    }

    /* 这是继承自Serializer接口的方法，
       用于获取要序列化成的数据的Writable子类型的Class，
       应为hive是支持hdfs的，
       所以序列化也是序列化成hadoop可写的writable格式。*/
    public Class<? extends Writable> getSerializedClass() {
        return Text.class;
    }
}
